﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MaterialDemo.MVC.Filters
{
    /// <summary>
    /// session校验
    /// </summary>
    public class MySessionAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (context.Filters.Any(u => u is MyAllAllowAttribute))
            {
                return;//如果方法上有MyAllAllowAttribute特性。连session登录验证都不需要,直接通过验证
            }
            else
            {
                if (context.HttpContext.Session.GetString("UserId") == null)
                {
                    //调回登录页
                    context.Result = new RedirectResult("/Login/Loginx");
                }
                else
                {
                    return;
                }
            }
        }
    }
}
